Fix issue 14859: CheckBox and RadioButton ignore explicit BackColor values in VisualStylesMode.Net11 - #14865
Fix issue 14859: CheckBox and RadioButton ignore explicit BackColor values in VisualStylesMode.Net11#14865SimonZhao888 wants to merge 3 commits into
Conversation
…alues in VisualStylesMode.Net11
There was a problem hiding this comment.
Pull request overview
This pull request fixes a rendering regression in the .NET 11 modern rendering path (VisualStylesMode.Net11) where CheckBox and RadioButton ignored explicitly set BackColor values by always painting the parent background.
Changes:
- Updated
CheckBoxModernAdapterandRadioButtonModernAdapterto fill the control background withBackColorwhenUseVisualStyleBackColor == falseandBackColoris fully opaque. - Kept
ParentBackgroundRenderer.Paint(...)for visual-style-backed and transparent/alpha backgrounds to preserve parent blending behavior. - Added regression tests for both controls to validate explicit
BackColorbehavior inVisualStylesMode.Net11, and adjusted existing accent tests to avoid being affected by the new background fill.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/CheckBoxModernAdapter.cs | Conditionally fills with explicit BackColor when visual style background is disabled and color is opaque. |
| src/System.Windows.Forms/System/Windows/Forms/Controls/Buttons/ButtonInternal/RadioButtonModernAdapter.cs | Same conditional background fill logic for RadioButton in modern Net11 mode. |
| src/test/unit/System.Windows.Forms/System/Windows/Forms/CheckBoxTests.cs | Adds regression test for explicit BackColor in Net11; updates accent test to set UseVisualStyleBackColor = true. |
| src/test/unit/System.Windows.Forms/System/Windows/Forms/RadioButtonTests.cs | Adds regression test for explicit BackColor in Net11; updates accent test to set UseVisualStyleBackColor = true. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/test/unit/System.Windows.Forms/System/Windows/Forms/RadioButtonTests.cs:224
- This test sets
BackColor = Color.Redand then counts pixels matchingApplication.SystemVisualSettings.AccentColoracross the whole bitmap. If the system accent color is red (or close enough for the 24-channel tolerance), the solid background will be counted as an accent pixel, making the unchecked case flaky. Consider using a BackColor derived from (and guaranteed to differ from) the accent color, and use that value for the background assertion.
using RadioButton control = new()
{
BackColor = Color.Red,
UseVisualStyleBackColor = true,
Checked = isChecked,
src/test/unit/System.Windows.Forms/System/Windows/Forms/CheckBoxTests.cs:586
- The test uses
BackColor = Color.Redand then searches the whole bitmap forApplication.SystemVisualSettings.AccentColor. If the Windows accent color happens to match red, the background fill will be counted as an accent pixel and can make theexpectedAccentassertion flaky (especially for the unchecked case). Use a BackColor derived from (and guaranteed to differ from) the accent color, and reuse that value in the background assertion.
using CheckBox box = new()
{
BackColor = Color.Red,
UseVisualStyleBackColor = true,
CheckState = checkState,
|
This looks good.
(Other than that, I would approve. I just want to see, if we would need to change something else based on the Padding/Margin outcome.) |
| Control.ClientRectangle, | ||
| Control.BackColor); | ||
| bool useControlBackColor = !Control.BackColor.HasTransparency() | ||
| && (Control.ShouldSerializeBackColor() || !Control.UseVisualStyleBackColor); |
There was a problem hiding this comment.
Explicit translucent BackColor values are still ignored here. ButtonBase supports transparent back colors, but when HasTransparency() is true this falls through to ParentBackgroundRenderer.Paint. With a parent, that helper paints only the parent background; Control.BackColor is merely a no-parent fallback and is never composited over it. For example, a 50%-alpha Aqua BackColor on a white parent still renders white. Please paint the parent first and then overlay the translucent control color. The same issue exists in RadioButtonModernAdapter.
| Color backgroundPixel = bitmap.GetPixel(box.Width - 2, box.Height / 2); | ||
| Assert.Equal(Color.Red.ToArgb(), backgroundPixel.ToArgb()); | ||
| Assert.Equal( | ||
| expectedAccent, |
There was a problem hiding this comment.
This assertion is environment-dependent because the entire control background is Color.Red. If the Windows Accent color is red, the unchecked case finds the background pixels and reports an accent glyph that was never drawn. Please choose a background color guaranteed to differ from Application.SystemVisualSettings.AccentColor (and make the equivalent change in RadioButtonTests).

Fixes #14859
Root Cause
The NET11 Visual Styles rendering path overrides the
BackColorexplicitly set by the user, causingCheckBoxandRadioButtoncontrols to deviate from the standard WinForms property precedence rules.Proposed changes
Customer Impact
Restore existing WinForms behavior, namely, when the user sets the BackColor for CheckBox and RadioButton, the controls will be drawn with reference to the set value.
Regression?
Risk
Screenshots
Before
After
DarkMode:
14865-DarkMode.mp4
Light:
14865-Light.mp4
Test methodology
Test environment(s)